// Copyright (c) 2011 The Kiwii Authors. All rights reserved.
// Use of this source code is governed by the New BSD License that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

#include "comm/Logger.hxx"
#include <time.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

using namespace kiwii::comm;
using namespace std;

Logger::Logger() : level_(INFO), logfile_(stdout) {
}

void Logger::log(Level level, char const* fmt, va_list ap) {
  static const char l[] = {'T', 'D', 'I', 'W', 'E', 'F'};
  char buffer[1024];
  char timestamp[128];

  time_t now = time(NULL);
  strftime(timestamp, sizeof(timestamp), "%d %b %Y %H:%M:%S", localtime(&now));
  vsnprintf(buffer, sizeof(buffer), fmt, ap);

  fprintf(logfile_, "[%d] %s [%c] %s\n", getpid(), timestamp, l[level], buffer);
  fflush(logfile_);
}

// int main() {
//   Logger& logger = Logger::get();
//   logger.error("error test");
//   logger.fatal("fatal test: %d", 100);
//   logger.warning("warning test: %s", "warning");
//   logger.info("info test: should not be logged");

//   Logger& logger2 = Logger::get();
//   logger2.setLevel(Logger::TRACE);
//   logger2.info("info test");
//   logger2.debug("debug test");
//   logger2.trace("trace test");

//   return 0;
// }
